package com.canaan.business.service.basic.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollectionUtil;
import com.canaan.business.domain.basic.WmsStorehouseLocation;
import com.canaan.business.domain.vo.StorehouseAreaNodeVo;
import com.canaan.business.domain.vo.StorehouseLocationNodeVo;
import com.canaan.business.domain.vo.StorehouseNodeVo;
import com.canaan.business.service.basic.IWmsStorehouseLocationService;
import com.canaan.business.service.basic.IWmsStorehouseService;
import com.canaan.common.exception.ServiceException;
import com.canaan.common.utils.DateUtils;
import com.canaan.common.utils.SecurityUtils;
import com.canaan.common.utils.StringUtils;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.canaan.business.mapper.basic.StorehousePlatMapper;
import com.canaan.business.domain.basic.StorehousePlat;
import com.canaan.business.service.basic.IStorehousePlatService;

/**
 * 仓库绘制地图信息Service业务层处理
 *
 * @author 
 * @date 2023-08-04
 */
@Service
public class StorehousePlatServiceImpl implements IStorehousePlatService {
    @Resource
    private StorehousePlatMapper storehousePlatMapper;
    @Resource
    private IWmsStorehouseService storehouseService;
    @Resource
    private IWmsStorehouseLocationService storehouseLocationService;

    /**
     * 查询仓库绘制地图信息
     *
     * @param id 仓库绘制地图信息主键
     * @return 仓库绘制地图信息
     */
    @Override
    public StorehousePlat selectStorehousePlatById(Long id) {
        return storehousePlatMapper.selectStorehousePlatById(id);
    }

    /**
     * 查询仓库绘制地图信息列表
     *
     * @param storehousePlat 仓库绘制地图信息
     * @return 仓库绘制地图信息
     */
    @Override
    public List<StorehousePlat> selectStorehousePlatList(StorehousePlat storehousePlat) {
        return storehousePlatMapper.selectStorehousePlatList(storehousePlat);
    }

    @Override
    public StorehousePlat getInfoByStorehouseNo(String storehouseNo) {
        StorehousePlat plat = new StorehousePlat();
        plat.setStorehouseNo(storehouseNo);
        List<StorehousePlat> platList = this.storehousePlatMapper.selectStorehousePlatList(plat);
        if (CollectionUtil.isNotEmpty(platList)){
            return platList.get(0);
        }
        return null;
    }

    /**
     * 新增仓库绘制地图信息
     *
     * @param storehousePlat 仓库绘制地图信息
     * @return 结果
     */
    @Override
    public int insertStorehousePlat(StorehousePlat storehousePlat) {
        StorehousePlat palt  = this.getInfoByStorehouseNo(storehousePlat.getStorehouseNo());
        if (palt!=null){
            throw new ServiceException("该仓库属性已存在");
        }
        storehousePlat.setCreateBy(SecurityUtils.getUsername());
        storehousePlat.setCreateTime(DateUtils.getNowDate());
        return storehousePlatMapper.insertStorehousePlat(storehousePlat);
    }

    /**
     * 批量新增仓库绘制地图信息
     *
     * @param storehousePlatList 仓库绘制地图信息列表
     * @return 结果
     */
    @Override
    public int batchInsertStorehousePlat(List<StorehousePlat> storehousePlatList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (StorehousePlat storehousePlat : storehousePlatList){
            storehousePlat.setCreateBy(username);
            storehousePlat.setCreateTime(currerTime);
        }
        int insertNum = 0;
        for (int i=0; i<storehousePlatList.size();) {
            int endIndex = i+500;
            if (endIndex > storehousePlatList.size()) {
                endIndex = storehousePlatList.size();
            }
            insertNum = insertNum + storehousePlatMapper.batchInsertStorehousePlat(storehousePlatList.subList(i, endIndex));
            i = endIndex;
        }
        return insertNum;
    }

    /**
     * 修改仓库绘制地图信息
     *
     * @param storehousePlat 仓库绘制地图信息
     * @return 结果
     */
    @Override
    public int updateStorehousePlat(StorehousePlat storehousePlat) {
        storehousePlat.setUpdateBy(SecurityUtils.getUsername());
        storehousePlat.setUpdateTime(DateUtils.getNowDate());
        return storehousePlatMapper.updateStorehousePlat(storehousePlat);
    }

    /**
     * 批量修改仓库绘制地图信息
     *
     * @param storehousePlatList 仓库绘制地图信息列表
     * @return 结果
     */
    @Override
    public int batchUpdateStorehousePlat(List<StorehousePlat> storehousePlatList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (StorehousePlat storehousePlat : storehousePlatList){
            storehousePlat.setUpdateBy(username);
            storehousePlat.setUpdateTime(currerTime);
        }
        int updateNum = 0;
        for (int i=0; i<storehousePlatList.size();) {
            int endIndex = i+500;
            if (endIndex > storehousePlatList.size()) {
                endIndex = storehousePlatList.size();
            }
            updateNum = updateNum + storehousePlatMapper.batchUpdateStorehousePlat(storehousePlatList.subList(i, endIndex));
            i = endIndex;
        }
        return updateNum;
    }

    /**
     * 批量删除仓库绘制地图信息
     *
     * @param ids 需要删除的仓库绘制地图信息主键集合
     * @return 结果
     */
    @Override
    public int deleteStorehousePlatByIds(Long[] ids) {
        return storehousePlatMapper.deleteStorehousePlatByIds(ids);
    }

    /**
     * 删除仓库绘制地图信息信息
     *
     * @param id 仓库绘制地图信息主键
     * @return 结果
     */
    @Override
    public int deleteStorehousePlatById(Long id) {
        return storehousePlatMapper.deleteStorehousePlatById(id);
    }

    @Override
    public List<StorehouseNodeVo> selectStoreHouseAllTree(String storeHouseNo) {
        List<StorehouseNodeVo> storehouseNodeVoList = Lists.newArrayList();
        WmsStorehouseLocation param = new WmsStorehouseLocation();
        param.setStorehouseNo(storeHouseNo);
        List<WmsStorehouseLocation> storehouseLocationList = this.storehouseLocationService.selectWmsStorehouseLocationList(param);
        if (CollectionUtil.isNotEmpty(storehouseLocationList)){
            Map<String,List<WmsStorehouseLocation>> storeHouseMap =  storehouseLocationList.stream().collect(Collectors.groupingBy(e->e.getStorehouseNo()));
            if (CollectionUtil.isNotEmpty(storeHouseMap)){
                for (String storeHoseNo:storeHouseMap.keySet()){
                    StorehouseNodeVo storehouseNodeVo = new StorehouseNodeVo();
                    storehouseNodeVo.setNo(storeHoseNo);
                    storehouseNodeVo.setCategory("1");
                    //获取该仓库下的所有库位信息
                    List<WmsStorehouseLocation> storeHouseList = storeHouseMap.get(storeHoseNo);
                    List<StorehouseAreaNodeVo> areaNodeVoList = Lists.newArrayList();
                    if (CollectionUtil.isNotEmpty(storeHouseList)){
                        //获取该仓库下的所有库区
                        Map<String,List<WmsStorehouseLocation>> areaMap =  storeHouseList.stream().collect(Collectors.groupingBy(e->e.getStorehouseAreaNo()));
                        if (CollectionUtil.isNotEmpty(areaMap)){
                            for (String areaNo:areaMap.keySet()){
                                StorehouseAreaNodeVo areaNodeVo = new StorehouseAreaNodeVo();
                                areaNodeVo.setNo(areaNo);
                                areaNodeVoList.add(areaNodeVo);
                                List<StorehouseLocationNodeVo> locationNodeVoList = Lists.newArrayList();
                                List<WmsStorehouseLocation> locationList = areaMap.get(areaNo);
                                if (CollectionUtil.isNotEmpty(locationList)){
                                    for (WmsStorehouseLocation storehouseLocation :locationList){
                                        StorehouseLocationNodeVo locationNodeVo = new StorehouseLocationNodeVo();
                                        locationNodeVo.setCategory("3");
                                        locationNodeVo.setNo(storehouseLocation.getNo());
                                        locationNodeVoList.add(locationNodeVo);
                                    }
                                }
                                areaNodeVo.setCategory("2");
                                areaNodeVo.setChildList(locationNodeVoList);
                            }

                        }
                    }
                    storehouseNodeVo.setChildList(areaNodeVoList);
                    storehouseNodeVoList.add(storehouseNodeVo);
                }
            }
        }
        return storehouseNodeVoList;
    }

    @Override
    public boolean uploadStoreHouse(String storeHouseNo, String storeHouseURL) {
        if (StringUtils.isNotEmpty(storeHouseNo)){
            StorehousePlat plat = this.getInfoByStorehouseNo(storeHouseNo);
            if (plat!=null){
                plat.setPlatImg(storeHouseURL);
                this.storehousePlatMapper.updateStorehousePlat(plat);
                return true;
            }
        }
        return false;
    }
}
